﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RegisterationCommon.Common;

namespace RegisterationCommon.School
{
    /// <summary>
    /// 学校帐号登录信息实体类
    /// </summary>
    public class SchoolAccount
    {
        #region 局部变量
        //主键，自动编号
        private int _ID = 0;
        //账号名称
        private string _AccountName = "";
        //密码（MD5加密）
        private string _AccountPswd = "";
        //是否超级账户(0是1不是)
        private bool _IsSuper = false;
        //是否禁用(0正常1禁用)
        private bool _IsDisable = false;
        //所属学校
        private int _SchID = 0;
        //是否是超级账号(0:普通账号，1：超级账号)
        private int _SuperAccount = 0;
        #endregion

        #region 属性列别
        /// <summary>
        /// 所属学校
        /// </summary>
        public int SchID
        {
            get { return _SchID; }
            set { _SchID = value; }
        }
        /// <summary>
        /// 是否禁用(0正常1禁用)
        /// </summary>
        public bool IsDisable
        {
            get { return _IsDisable; }
            set { _IsDisable = value; }
        }
        /// <summary>
        /// 是否超级账户(0是1不是)
        /// </summary>
        public bool IsSuper
        {
            get { return _IsSuper; }
            set { _IsSuper = value; }
        }
        /// <summary>
        /// 密码（MD5加密）
        /// </summary>
        public string AccountPswd
        {
            get { return _AccountPswd; }
            set { _AccountPswd = value; }
        }
        /// <summary>
        /// 账号名称
        /// </summary>
        public string AccountName
        {
            get { return _AccountName; }
            set { _AccountName = value; }
        }
        /// <summary>
        /// 主键，自动编号
        /// </summary>
        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        /// <summary>
        /// 是否是超级账号(0:普通账号，1：超级账号)
        /// </summary>
        public int SuperAccount
        {
            get { return _SuperAccount; }
            set { _SuperAccount = value; }
        }
        #endregion

        #region 静态成员
        //获取当前账号实例
        private static SchoolAccount _Current;
        /// <summary>
        /// 获取当前账号实例
        /// </summary>
        public static SchoolAccount Current
        {
            get
            {
                if (_Current == null)
                {
                    SchoolAccount._Current = new SchoolAccount();
                }
                return SchoolAccount._Current;
            }
        }
        #endregion

        #region 方法列表

        #region 账号登录验证 保存cookie状态
        /// <summary>
        /// 执行账号登录验证 保存cookie状态
        /// </summary>
        /// <param name="schName">账号</param>
        /// <param name="schPwd">MD5加密后的密码</param>
        /// <returns>登录成功返回true,否则返回false</returns>
        public bool Login(string schName, string schPwd)
        {
            //非法字符验证
            if (!BaseLibrary.Verification(schName, false, EVerification.SafeString) || !BaseLibrary.Verification(schPwd, false, EVerification.SafeString))
            {
                return false;
            }
            //查询语句
            string sql = "select * from " + ETableName.SchoolAccount + " where AccountName='" + schName + "' and AccountPswd='" + schPwd + "'";
            DataTable dt = DBAction.GetDTFromSQL(sql);
            if (dt.Rows.Count < 1)
            {
                return false;
            }
            //保存到变量里
            this.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
            this._AccountName = Convert.ToString(dt.Rows[0]["AccountName"]);
            this._AccountPswd = Convert.ToString(dt.Rows[0]["AccountPswd"]);
            this._IsSuper = Convert.ToBoolean(dt.Rows[0]["IsSuper"]);
            this._IsDisable = Convert.ToBoolean(dt.Rows[0]["IsDisable"]);
            this._SchID = Convert.ToInt32(dt.Rows[0]["SchID"]);
            this._SuperAccount = Convert.ToInt32(dt.Rows[0]["SuperAccount"]);
            Cookies.Set("SchoolAccountName", this._AccountName);
            Cookies.Set("SchoolAccountPswd", this._AccountPswd);
            Cookies.Set("IsSuper", Convert.ToString(this._IsSuper).ToString());
            Cookies.Set("IsDisable", Convert.ToString(this._IsDisable).ToString());
            Cookies.Set("SchID", Convert.ToString(this._SchID).ToString());
            Cookies.Set("ID", Convert.ToString(this._ID).ToString());
            Cookies.Set("SchoolSuperAccount", Convert.ToString(this._SuperAccount).ToString());
            return true;
        }
        #endregion

        #region 判断当前账号是否登录状态
        /// <summary>
        /// 判断账号是否登录状态，并且账号信息是否安全
        /// </summary>
        /// <returns></returns>
        public bool IsLogin()
        {
            bool isLogin = false;
            if (Cookies.Exists())
            {
                string sql = "select * from " + ETableName.SchoolAccount + " where AccountName='" + Cookies.Get("SchoolAccountName") + "' and AccountPswd='" + Cookies.Get("SchoolAccountPswd") + "'";
                DataTable dt = DBAction.GetDTFromSQL(sql);
                if (dt.Rows.Count < 1)
                {
                    isLogin = false;
                    return isLogin;
                }
                isLogin = true;
            }

            return isLogin;
        }
        #endregion

        #region 退出系统
        /// <summary>
        /// 退出系统
        /// </summary>
        public void Logout()
        {
            Cookies.Clear();
            Message.Send(10014);
        }
        #endregion

        #region 修改账号密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="accountID">账号ID</param>
        /// <param name="accountID">MD5加密后的账号密码</param>
        /// <returns>修改成功返回true,否则返回false</returns>
        public bool MdfSchoolAccountPwd(int schID, string schPwd)
        {
            bool isSucceed = false;

            //非法字符验证
            if (!BaseLibrary.Verification(schPwd, false, EVerification.SafeString))
            {
                isSucceed = false;
            }

            RParams RP = new RParams();
            RP.Add("ID", schID, SqlDbType.Int);
            RP.Add("AccountPswd", schPwd, SqlDbType.VarChar);

            string colFields = "AccountPswd";
            string whereFields = "ID=@ID";


            if (DBAction.Update(ETableName.SchoolAccount, colFields, whereFields, RP))
            {
                Cookies.Set("AccountPswd", schPwd);
                isSucceed = true;
            }
            else
            {
                isSucceed = false;
            }
            return isSucceed;
        }
        /// <summary>
        /// 修改当前登录用户密码
        /// </summary>
        /// <returns>修改成功返回true,否则返回false</returns>
        public bool MdfSchoolAccountPwd()
        {
            return MdfSchoolAccountPwd(Convert.ToInt32(Cookies.Get("ID").ToString()), this._AccountPswd);
        }
        #endregion

        #region 修改账号状态
        /// <summary>
        /// 修改账号状态
        /// </summary>
        /// <param name="accountID">账号ID</param>
        /// <param name="accountID">MD5加密后的账号密码</param>
        /// <returns>修改成功返回true,否则返回false</returns>
        public bool MdfSchoolAccountStatus()
        {
            RParams RP = new RParams();
            RP.Add("ID", _ID, SqlDbType.Int);
            RP.Add("IsDisable", _IsDisable, SqlDbType.VarChar);

            string colFields = "IsDisable";
            string whereFields = "ID=@ID";
            return MdfSchoolAccount(colFields, whereFields, RP);
        }
        #endregion
        #region 修改账号状态
        /// <summary>
        /// 重置账号密码
        /// </summary>
        /// <param name="accountID">账号ID</param>
        /// <param name="accountID">MD5加密后的账号密码</param>
        /// <returns>修改成功返回true,否则返回false</returns>
        public bool MdfSchoolAccountPswd()
        {
            RParams RP = new RParams();
            RP.Add("ID", _ID, SqlDbType.Int);
            RP.Add("AccountPswd", _AccountPswd, SqlDbType.VarChar);

            string colFields = "AccountPswd";
            string whereFields = "ID=@ID";
            return MdfSchoolAccount(colFields, whereFields, RP);
        }
        #endregion
        #region 修改学校账号信息

        /// <summary>
        /// 修改学校账号信息
        /// </summary>
        /// <returns>修改成功返回true,否则返回false</returns>
        public bool MdfSchoolAccount()
        {
            RParams RP = new RParams();
            RP.Add("ID", this._ID, SqlDbType.Int);
            RP.Add("AccountName", this._AccountName, SqlDbType.VarChar);
            RP.Add("AccountPswd", this._AccountPswd, SqlDbType.VarChar);
            RP.Add("IsSuper", this._IsSuper, SqlDbType.Bit);
            RP.Add("IsDisable", this._IsDisable, SqlDbType.Bit);
            RP.Add("SchID", this._SchID, SqlDbType.Int);
            string colFields = "IsSuper,IsDisable,SchID";
            string whereFields = "ID=@ID";
            return MdfSchoolAccount(colFields, whereFields, RP);
        }
        /// <summary>
        /// 修改学校账号信息
        /// </summary>
        /// <param name="colFields">修改的列</param>
        /// <param name="whereFields">修改条件</param>
        /// <param name="RP">数据集</param>
        ///  <returns>修改成功返回true,否则返回false</returns>
        public bool MdfSchoolAccount(string colFields, string whereFields, RParams RP)
        {
            return DBAction.Update(ETableName.SchoolAccount, colFields, whereFields, RP);
        }
        #endregion

        #region 添加学校帐号

        /// <summary>
        /// 添加学校帐号
        /// </summary>
        /// <returns>添加成功返回true,否则返回false</returns>
        public bool AddSchoolAccount()
        {
            RParams RP = new RParams();
            RP.Add("AccountName", this._AccountName, SqlDbType.VarChar);
            RP.Add("AccountPswd", this._AccountPswd, SqlDbType.VarChar);
            RP.Add("IsSuper", this._IsSuper, SqlDbType.Bit);
            RP.Add("IsDisable", this._IsDisable, SqlDbType.Bit);
            RP.Add("SchID", this._SchID, SqlDbType.Int);
            RP.Add("SuperAccount", this._SuperAccount, SqlDbType.Int);
            return AddSchoolAccount(RP);
        }
        /// <summary>
        /// 添加学校帐号
        /// </summary>
        /// <param name="RP">数据集</param>
        /// <returns>添加成功返回true,否则返回false</returns>
        public bool AddSchoolAccount(RParams RP)
        {
            return DBAction.Insert(ETableName.SchoolAccount, RP);
        }
        #endregion

        #region 根据ID删除学校帐号
        /// <summary>
        /// 根据ID删除学校帐号
        /// </summary>
        /// <param name="schID">学校帐号ID</param>
        /// <returns>删除成功返回true,否则返回false</returns>
        public bool DelSchoolAccount(int schID)
        {
            RParams RP = new RParams();
            RP.Add("ID", schID, SqlDbType.Int);
            string whereFields = "ID=@ID";
            return DBAction.Delete(ETableName.SchoolAccount, whereFields, RP);
        }
        /// <summary>
        /// 根据ID删除学校帐号
        /// </summary>
        /// <returns>删除成功返回true,否则返回false</returns>
        public bool DelSchoolAccount()
        {
            return DelSchoolAccount(this._ID);
        }
        #endregion

        #region 分页获取学校下面的帐号
        /// <summary>
        /// 分页获取学校下面的帐号
        /// </summary>
        /// <param name="RP">数据集</param>
        /// <returns>返回JSON格式的字符串</returns>
        public string GetSchoolAccountListBySchID(RParams RP)
        {
            //查询当前页
            DataTable dt = DBAction.ExecSP("sp_select_page", RP).Tables[0];
            //查询条件的总条数
            int totalCount = DBAction.GetDTFromSQL("select * from " + ETableName.SchoolAccount + " where " + RP.GetSqlParameter("Where").Value).Rows.Count;
            //返回Json
            return DBAction.DataTableToJson(dt, true, totalCount);
        }
        #endregion
        #endregion
    }
}
